/// <reference path="../_all.ts" />

module Application.Controllers {

    export class ActionsController implements Application.Interfaces.Controllers.IController {

        _server_id: number;
        _timeout_id: any;
        _timeout_delay: number;

        public static $inject = [
            '$rootScope',
            '$scope',
            '$location',
            '$routeParams',
            '$timeout',
            'ActionService',
            'ServerService',
            'TranslationService',
        ];

        actionsResult: {
            (data): void;
            (data, err): void;
        };

        serverResult: {
            (data): void;
            (data, err): void;
        };

        constructor(public RootScope: Application.Interfaces.Scopes.IRootScope,
            public Scope: Application.Interfaces.Scopes.IActionsScope,
            public Location: ng.ILocationService,
            public RouteParams: Application.Interfaces.Routing.IServerRouteParams,
            public TimeOut: ng.ITimeoutService,
            public Actions: Application.Interfaces.Services.IActionService,
            public Server: Application.Interfaces.Services.IServerService,
            public Translations: Application.Interfaces.Services.ITranslationService) {

                this.Scope.vm = this;
                this.Scope.user = this.RootScope.user;
                this.Scope.location = this.Location;
                this.Scope.messages = new Application.Utility.Messages();
                this.Scope.navigations = new Application.Utility.Navigations();
                this.Scope.server = <Application.Resources.IServer>{};
                this.Scope.actions = <Application.Resources.IAction[]>[];

                this._server_id = 0;
                if (this.RouteParams.server_id) {
                    this._server_id = parseInt(this.RouteParams.server_id.toString());
                };

                this.Scope.navigations.AddLink(Translations.Translate("SERVERLIST"), "/servers", false, false, Application.Constants.Icons.Servers);

                if (this._server_id > 0) {
                    this.Scope.navigations.AddLink(Translations.Translate("SERVER"), "/server/" + this._server_id, false, false, Application.Constants.Icons.Server);
                    this.Scope.navigations.AddLink(Translations.Translate("SERVERACTIONS"), "/actions/" + this._server_id, true, true), Application.Constants.Icons.Actions;
                    this.Scope.navigations.AddLink(Translations.Translate("SERVERRIGHTS"), "/members/" + this._server_id, false, true, Application.Constants.Icons.Rights);
                } else {
                    this.Scope.navigations.AddLink(Translations.Translate("ACTIONS"), "/actions", true, false, Application.Constants.Icons.Actions);
                };
                if (this.Scope.user && this.Scope.user.role && this.Scope.user.role >= 2) {
                    this.Scope.navigations.AddLink(Translations.Translate("PLAYERS"), "/players", false, false, Application.Constants.Icons.Bans);
                };
                if (this.Scope.user && this.Scope.user.role && this.Scope.user.role >= 8) {
                    this.Scope.navigations.AddLink(Translations.Translate("USERS"), "/users", false, false, Application.Constants.Icons.Users);
                };
                this.Scope.navigations.AddLinkRight(Translations.Translate("ACCOUNT"), "/users/" + this.Scope.user.user_id, false);
                this.Scope.navigations.AddLinkRight(Translations.Translate("LOGOUT"), "/", false);

                this.actionsResult = (data, err?) => {
                    this.actionsResultProxy.apply(this, [data, err]);
                }

                this.serverResult = (data, err?) => {
                    this.serverResultProxy.apply(this, [data, err]);
                };

                if (this._server_id > 0) {
                    this.getServer();
                } else {
                    this.getActions();
                };
        }

        public log(msg: string) {
            //console.log(msg);
        }

        public refresh() {
            if (this.Scope.server != null && this._server_id && this._server_id > 0) {
                this.getServer();
            } else {
                this.getActions();
            };
        }

        public getActions() {
            this.log('getActions');
            if (this.Scope.server != null && this._server_id && this._server_id > 0) {
                this.Actions.GetServerActions(this._server_id, this.actionsResult);
            } else {
                this.Actions.GetActions(this.actionsResult);
            };
        }

        public closeAction(action: Application.Resources.IAction) {
            this.log('closeAction');
            if (action != null && action.action_id && action.action_id > 0) {
                this.Actions.CloseAction(action, this.actionsResult);
                action.is_executed = true;
            } else {
                this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, "Error", this.Translations.Translate('INVALIDACTION'));
            };
        }

        private actionsResultProxy(data, err) {
            this.log('actions result');
            if (data != null) {
                var actions = <Application.Resources.IAction[]> data;
                if (actions != null && actions.length > 0) {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.success, "Info", new Application.Utility.String(this.Translations.Translate('ACTIONSFOUND')).format(actions.length.toString()));
                    var result: Application.Resources.IAction;
                    for (var i = 0; i < this.Scope.actions.length; i++) {
                        for (var j = 0; j < actions.length; j++) {
                            if (this.Scope.actions[i].action_id == actions[j].action_id) {
                                result = actions[j];
                                Application.Resources.ActionUtility.fixBooleans(result);
                                actions.splice(j, 1);
                                break;
                            };
                        };
                        if (result == null) {
                            this.Scope.actions.splice(i, 1);
                            i--;
                        } else {
                            this.Scope.actions[i] = result;
                        };
                    }; 
                    for (var i = 0; i < actions.length; i++) {
                        result = actions[i];
                        if (result.user) {
                            result.user_name = result.user.login;
                        };
                        Application.Resources.ActionUtility.fixBooleans(result);
                        this.Scope.actions.push(result);
                    };
                } else {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, "Error", this.Translations.Translate('ACTIONSNOTFOUND'));
                };
            } else {
                if (err != null) {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, err.data.status + ' ' + err.data.type, err.data.message);
                } else {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, '000 Unknown', this.Translations.Translate('UNKNOWNERROR'));
                };
            };
        }

        public getServer() {
            if (this._server_id != null) {
                this.log('getServer: ' + this._server_id);
                this.Server.GetServer(this._server_id, this.serverResult);
            } else {
                this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, "Error", this.Translations.Translate('NOSERVERID'));
            };
        }

        private serverResultProxy(data, err) {
            this.log('server result');
            if (data != null) {
                var server = <Application.Resources.IServer> data;
                if (server != null) {
                    Application.Resources.ServerUtility.fixBooleans(server);
                    this.Scope.server = server;
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.info, "Status", this.Translations.Translate('SERVERINFOLOADED'));
                    this.getActions();
                } else {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, "Error", this.Translations.Translate('SERVERNOTFOUND'));
                };
            } else {
                if (err != null) {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, err.data.status + ' ' + err.data.type, err.data.message);
                } else {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, '000 Unknown', this.Translations.Translate('UNKNOWNERROR'));
                };
            };
        }

    };
}